// Sunghan Cho
// 301140084
// sunghanc@sfu.ca

#include "itemlistmodel.h"
#include "QDebug"

ItemListModel::ItemListModel(QObject *parent) : QAbstractTableModel(parent)
{
}

ItemListModel::ItemListModel(ItemManager *pItemManager, QObject *parent) : QAbstractTableModel(parent)
{
    m_pItemManager = pItemManager;

    QObject::connect(m_pItemManager, SIGNAL(itemsChanged()), this, SLOT(updateItems()));
}

int ItemListModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    //qDebug()<<"rowCount: "<<m_items.count();
    return m_items.count();
}

int ItemListModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    foreach(Item* itr, m_items){
        if(itr->getFieldCount() == 2){
            //qDebug()<<"columnCount: 2";
            return 2;
        }
        if(itr->getFieldCount() == 3){
            //qDebug()<<"columnCount: 3";
            return 3;
        }
    }
    //qDebug()<<"columnCount: 0";
    return 0;
}

QVariant ItemListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(role != Qt::DisplayRole){
        //qDebug()<<"Error in itemListMode::headerData --> role != Qt::DisplayRole";
        return QVariant();
    }

    if(m_items.size() == 0){
        //qDebug()<<"Error in itemListModel::headerData --> m_items.size == 0";
        return QVariant();
    }

    if(orientation == Qt::Horizontal){
        foreach(Item* itr, m_items){
            switch(section){
            case 0:
                //qDebug()<<"headerData: "<<itr->getFieldHeading(0);
                return itr->getFieldHeading(0);
            case 1:
                //qDebug()<<"headerData: "<<itr->getFieldHeading(1);
                return itr->getFieldHeading(1);
            case 2:
                //qDebug()<<"headerData: "<<itr->getFieldHeading(2);
                return itr->getFieldHeading(2);
            default:
                return QVariant();
            }
        }
    }
    return QVariant();
}

QVariant ItemListModel::data(const QModelIndex &index, int role) const
{
    int row = index.row();
    int col = index.column();

    if(!index.isValid()){
        return QVariant();
    }

    if(role == Qt::DisplayRole){
        if(col == 0){
            return m_items.at(row)->getFieldValue(0);
        }
        if(col == 1){
            return m_items.at(row)->getFieldValue(1);
        }
        if(col == 2){
            return m_items.at(row)->getFieldValue(2);
        }
    }

    return QVariant();
}

bool ItemListModel::shouldIncludeInList(Item *pNewItem)
{
    Q_UNUSED(pNewItem);
    return true;
}

void ItemListModel::updateItems()
{
    //qDebug()<<"ItemListModel::updateItems() SLOT recieved";
    emit layoutAboutToBeChanged();
    //qDebug()<<"layoutAboutToBeChanged() is called";

    this->m_items.clear();

    QListIterator<Item*> itr = m_pItemManager->getIterator();

    while(itr.hasNext()){
        Item* item = itr.next();
        if(this->shouldIncludeInList(item)){
            //qDebug()<<"m_items size after clean and before new item copied: "<<m_items.size();
            //qDebug()<<"Item copied from m_items(itemManager) to m_items(itemListModel): "<<item->getFieldValue(0).toString();
            this->m_items.push_back(item);
        }
    }
    //qDebug()<<"m_items size after push_back: "<<m_items.size();
    emit layoutChanged();
    //qDebug()<<"layoutChanged() is called";
}

void ItemListModel::setSelectedItem(int itemIndex)
{
    if(itemIndex < 0){
        emit 0;
    }
    emit selectedItemChanged(m_items.at(itemIndex));
    //qDebug()<<"ItemListModel::setSelectedItem SLOT recieved and emit selectedItemChanged SIGNAL";
}
